package cn.ffcs.uec.staff.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.staff.dao.UecStaffInfoDao;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uec.staff.vo.UecStaffInfoVo;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecStaffInfoDao")
@Transactional
public class UecStaffInfoDaoImpl extends BaseDaoImpl implements UecStaffInfoDao {

	@Override
	public PageInfo queryPageInfoByUecStaffInfo(UecStaffInfo uecStaffInfo, int current, int pageSize) {
		String orgName = PermissionUtil.getManagerOrg();
		if (!StrUtil.isNullOrEmpty(orgName)) {
			StringBuilder sb = new StringBuilder(
					" SELECT T.* FROM UEC_STAFF_INFO T left join UEC_ORG_ORGS t2 on (T.PK_ORG = T2.PK_ORG) WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? AND T2.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			if (null != uecStaffInfo) {
				if (!StrUtil.isNullOrEmpty(uecStaffInfo.getCode())) {
					sb.append(" AND T.CODE = ? ");
					params.add(uecStaffInfo.getCode());
				}
				if (!StrUtil.isNullOrEmpty(uecStaffInfo.getName())) {
					sb.append(" AND T.NAME LIKE ? ");
					params.add("%" + StringEscapeUtils.escapeSql(uecStaffInfo.getName()) + "%");
				}
				if (!StrUtil.isNullOrEmpty(uecStaffInfo.getIdNumber())) {
					sb.append(" AND T.IDNUMBER = ? ");
					params.add(uecStaffInfo.getIdNumber());
				}
				if (!StrUtil.isNullOrEmpty(uecStaffInfo.getPkOrg())) {
					sb.append(" AND T.PK_ORG = ? ");
					params.add(uecStaffInfo.getPkOrg());
				}
				if (!"区本部".equals(orgName)) {
					sb.append(" AND T2.NAME LIKE ? ");
					params.add("%" + StringEscapeUtils.escapeSql(orgName) + "%");
				}
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current, pageSize, UecStaffInfo.class);
		}
		return null;
	}

	@Override
	public UecStaffInfo findUecStaffInfo(UecStaffInfo uecStaffInfo) {
		StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_STAFF_INFO T  WHERE T.status_cd = ? ");
		List<Object> params = new ArrayList<Object>();
		params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
		if (uecStaffInfo != null) {
			if(!StrUtil.isNullOrEmpty(uecStaffInfo.getPkPsndoc())) {
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecStaffInfo.getPkPsndoc());
			}
			if (!StrUtil.isNullOrEmpty(uecStaffInfo.getName())) {
				sb.append(" AND T.NAME LIKE ? ");
				params.add("%" + StringEscapeUtils.escapeSql(uecStaffInfo.getName()) + "%");
			}
			if (!StrUtil.isNullOrEmpty(uecStaffInfo.getIdNumber())) {
				sb.append(" AND T.IDNUMBER = ? ");
				params.add(uecStaffInfo.getIdNumber());
			}
			if (!StrUtil.isNullOrEmpty(uecStaffInfo.getCode())) {
				sb.append(" AND T.CODE = ? ");
				params.add(uecStaffInfo.getCode());
			}
			List<UecStaffInfo> list = super.jdbcFindList(sb.toString(), params, UecStaffInfo.class);
			if (list.size() > 0 && list != null) {
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public List<UecStaffInfo> findUecStaffInfoList(UecStaffInfo uecStaffInfo) {
		StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_STAFF_INFO T  WHERE T.status_cd = ? ");
		List<Object> params = new ArrayList<Object>();
		params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
		if (uecStaffInfo != null) {
			if (!StrUtil.isNullOrEmpty(uecStaffInfo.getName())) {
				sb.append(" AND T.NAME LIKE ? ");
				params.add("%" + StringEscapeUtils.escapeSql(uecStaffInfo.getName()) + "%");
			}
			if (!StrUtil.isNullOrEmpty(uecStaffInfo.getIdNumber())) {
				sb.append(" AND T.IDNUMBER = ? ");
				params.add(uecStaffInfo.getIdNumber());
			}
			List<UecStaffInfo> list = super.jdbcFindList(sb.toString(), params, UecStaffInfo.class);
			if (list.size() > 0 && list != null) {
				return list;
			}
		}
		return null;
	}

	@Override
	public UecStaffInfo findUecStaffInfoOnly(String idName, String idNumber) {
		StringBuilder sb = new StringBuilder(
				" SELECT * FROM UEC_STAFF_INFO T  WHERE T.status_cd = ? AND T.NAME = ? AND T.IDNUMBER = ?");
		List<Object> params = new ArrayList<Object>();
		if (null != idName && null != idNumber) {
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			params.add(idName);
			params.add(idNumber);
			List<UecStaffInfo> list = super.jdbcFindList(sb.toString(), params, UecStaffInfo.class);
			if (list.size() > 0 && list != null) {
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public UecStaffInfo quaryUecStaffInfoByCode(String code) {
		StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_STAFF_INFO T  WHERE T.status_cd = ? AND T.CODE = ? ");
		List<Object> params = new ArrayList<Object>();
		if (null != code) {
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			params.add(code);
			UecStaffInfo uecStaffInfo = super.jdbcFindObject(sb.toString(), params, UecStaffInfo.class);
			return uecStaffInfo;
		}
		return null;
	}

	@Override
	public UecStaffInfo quaryUecStaffInfoByStaffId(Long staffId) {
		StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_STAFF_INFO T  WHERE 1=1 AND T.staff_id = ? ");
		List<Object> params = new ArrayList<Object>();
		if (null != staffId) {
			// params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			params.add(staffId);
			UecStaffInfo uecStaffInfo = super.jdbcFindObject(sb.toString(), params, UecStaffInfo.class);
			return uecStaffInfo;
		}
		return null;
	}

	@Override
	public PageInfo queryPageInfoByUecStaffInfoVo(UecStaffInfoVo uecStaffInfoVo, int current, int pageSize) {
		if(uecStaffInfoVo != null) {
			String orgName = PermissionUtil.getManagerOrg();
			StringBuilder sb = new StringBuilder(" SELECT * FROM  VIE_UEC_STAFF_INFO T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			if (!StrUtil.isNullOrEmpty(uecStaffInfoVo.getStaffName())) {
				sb.append(" AND T.staffName LIKE ? ");
				params.add("%"
						+ StringEscapeUtils.escapeSql(uecStaffInfoVo
								.getStaffName()) + "%");
			}
			if (!StrUtil.isNullOrEmpty(uecStaffInfoVo.getStaffCode())) {
				sb.append(" AND T.staffCode = ? ");
				params.add(uecStaffInfoVo.getStaffCode());
			}
			if (!StrUtil.isNullOrEmpty(uecStaffInfoVo.getPkOrg())) {
				sb.append(" AND T.pkOrg = ? ");
				params.add(uecStaffInfoVo.getPkOrg());
			}
			if (!StrUtil.isNullOrEmpty(uecStaffInfoVo.getPkDept())) {
				sb.append(" AND T.pkDept = ? ");
				params.add(uecStaffInfoVo.getPkDept());
			}
			if (!"区本部".equals(orgName)) {
				sb.append(" AND T.orgName LIKE ? ");
				params.add("%" + StringEscapeUtils.escapeSql(orgName) + "%");
			}
			
			if(!StrUtil.isNullOrEmpty(uecStaffInfoVo.getOrgName())) {
				if(uecStaffInfoVo.getOrgName().endsWith("*")) {
            		sb.append(" AND T.orgName like ? ");
            		params.add("%"+uecStaffInfoVo.getOrgName().substring(0, 2)+"%");
            	}
            	else {
            		sb.append(" AND T.orgName = ? ");
            		params.add(uecStaffInfoVo.getOrgName());
            	}
			}
			//sb.append(" ORDER BY T.joinjobdate DESC ");
			sb.append(" ORDER BY T.orgName ");
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecStaffInfoVo.class);
		}
		return null;
	}

}
